//
//  ZLViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "ZLViewController.h"
#import "ZLCollectionReusableView.h"
#import "ZLCollectionViewCell.h"
#import "WJQNetwork.h"
#import "ZLTitleModel.h"
#import "ZLDataMOdel.h"//数据Model
#import "ZLBannersModel.h"//轮播图Model
#import "ZLDetailViewController.h"//详情
#import "ZLMoreViewController.h"//更多
#import "ZLBannersModel.h"
#import "CycleScrollView.h"
#import "ZlCycleImageViewController.h"

@interface ZLViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,ZLCollectionReusableViewDelegate,CycleScrollViewDelegate>

@property (nonatomic, strong)UICollectionView *myCollectionView;
@property (nonatomic, strong)NSMutableArray *myArray;
@property (nonatomic, strong)NSMutableArray *titleArray;
@property (nonatomic, strong)NSMutableArray *headerImageArray;
@property (nonatomic, strong)CycleScrollView *cycleScrollV;
@property (nonatomic, strong)NSMutableArray *cycleImageArray;
@end

@implementation ZLViewController
//tabbar出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    view.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"目的地";
    self.navigationItem.leftBarButtonItem = nil;
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    self.cycleImageArray = [NSMutableArray array];
    self.headerImageArray = [NSMutableArray array];
    self.myArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
   
    [self createCollectionView];
    //显示菊花
    [self MBShow];
    
    [self getData];
    
    
    // 是否第一次启动
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirst"];
        
    }

    
    
    
    
    
    
    
}
#pragma mark - 创建CollectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.sectionInset = UIEdgeInsetsMake(5*AUTO_HEIGHT, 10*AUTO_WIDTH, 10*AUTO_HEIGHT, 10*AUTO_WIDTH);
    
    //行 列 间距
    flowLayout.minimumLineSpacing = 5*AUTO_HEIGHT;
    flowLayout.minimumInteritemSpacing = 5*AUTO_WIDTH;
//    //设置header大小
//    flowLayout.headerReferenceSize = CGSizeMake((SCREEN_WIDTH - 40*SCREEN_WIDTH/375)/2, (SCREEN_WIDTH - 40*SCREEN_WIDTH/375)/2);
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    [self.view addSubview:self.myCollectionView];
//    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.left.right.equalTo(self.view);
//        make.top.equalTo(@180);
//        make.height.mas_equalTo((SCREEN_WIDTH + 15*SCREEN_WIDTH/375)/2);
//    }];
    
#warning 轮播图
    self.myCollectionView.contentInset = UIEdgeInsetsMake(180*AUTO_HEIGHT, 0, 0, 0);
    self.cycleScrollV = [[CycleScrollView alloc]initWithFrame:CGRectMake(0, -180*AUTO_HEIGHT, SCREEN_WIDTH, 180 * AUTO_HEIGHT)];
    [self.myCollectionView addSubview:self.cycleScrollV];
    self.cycleScrollV.delegate = self;
    

    [self.myCollectionView registerClass:[ZLCollectionViewCell class] forCellWithReuseIdentifier:@"ZLCollectionViewCell"];
    [self.myCollectionView registerClass:[ZLCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZLCollectionReusableView"];
    
    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
}
#pragma mark - CollectionView协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.titleArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.titleArray objectAtIndex:section] datas].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLCollectionViewCell" forIndexPath:indexPath];
    ZLDataMOdel *zlTitleM = [[[self.titleArray objectAtIndex:indexPath.section] datas] objectAtIndex:indexPath.row];
    
    cell.zlDataM = zlTitleM;
    
    return cell;
}
#pragma mark - 头部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        ZLCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ZLCollectionReusableView" forIndexPath:indexPath];
        reusableView.titleM = [self.titleArray objectAtIndex:indexPath.section];
        //*******头视图 更多 签订代理人
        reusableView.collectionReusableViewDelegate = self;
        
        return reusableView;
    }else{
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        
        return reusableView;
    }
}

#pragma mark - 实现 "更多"协议方法
- (void)toMoreController:(NSInteger)tagInadex index:(NSInteger)index title:(NSString *)title
{
    ZLMoreViewController *MoreVC = [ZLMoreViewController new];
    MoreVC.index = index;
    MoreVC.titles = title;
    [self.navigationController pushViewController:MoreVC animated:YES];
}


#pragma mark - 返回高度
//首部高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 40*AUTO_HEIGHT);
}
//尾部高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == (self.titleArray.count - 1)) {
        return CGSizeMake(SCREEN_WIDTH, 55);
    }else{
        return CGSizeZero;
    }
}

//item
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 27*AUTO_WIDTH)/2, (SCREEN_WIDTH - 25*AUTO_WIDTH)/2);
}


#pragma mark - 网络请求
- (void)getData
{
    NSString *urlStr = @"http://api.breadtrip.com/destination/v3/";
    [WJQNetwork getAFNet:urlStr block:^(id result) {
       
        NSMutableDictionary *dic = result;
        NSMutableArray *bannersArray = [dic objectForKey:@"banners"];
        for (NSMutableDictionary *bannersDic in bannersArray) {
            ZLBannersModel *zlBanModel = [ZLBannersModel new];
            [zlBanModel setValuesForKeysWithDictionary:bannersDic];
            [self.headerImageArray addObject:zlBanModel.image_url];
            [self.cycleImageArray addObject:zlBanModel.html_url];
        }
        
        NSMutableArray *arr = [dic objectForKey:@"elements"];
        for (NSMutableDictionary *titleDic in arr) {
            ZLTitleModel *zlTitleM = [ZLTitleModel new];
            [zlTitleM setValuesForKeysWithDictionary:titleDic];
            [self.titleArray addObject:zlTitleM];
        }
        [self.cycleScrollV createCycleScrollViews:self.headerImageArray];
//        NSLog(@"%@",[[[[self.titleArray objectAtIndex:0]datas] objectAtIndex:0] name_en] );
        [self MBHide];
        [self.myCollectionView reloadData];
        
    } errorBlock:^(NSError *error) {
        NSLog(@"请求错误");
        
    }];
}


#pragma mark - 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLDetailViewController *detailVC = [ZLDetailViewController new];
    ZLDataMOdel *zldataM = [[[self.titleArray objectAtIndex:indexPath.section] datas] objectAtIndex:indexPath.row];
    
    detailVC.zlDataM = zldataM;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark - 轻拍轮播图 协议
- (void)getTapNumber:(NSInteger)number
{
    ZlCycleImageViewController *cycleImageVC = [ZlCycleImageViewController new];
    cycleImageVC.urlStr = self.cycleImageArray[number - 1];
    [self.navigationController pushViewController:cycleImageVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
